<div class="houtai-item-list">
    <table>
        <thead>
        <tr>
            <th>序号</th>
            <th>订单图片</th>
            <th>购买用户</th>
            <th>总价</th>
            <th>订单操作</th>
        </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
    <div class="page-under">

    </div>
</div>
<script type="text/javascript">
    $(function (){
        $(".page-under").load(`/shop/page`,function (){
            doGetItems();
        });
    });
    function doGetItems(){
        let url =`/shop/getOrders`;
        let pageCurrent = $(".page-under").data("pageCurrent");
        if (!pageCurrent)pageCurrent=1;
        let data = {"pageCurrent":pageCurrent};
        $.getJSON(url,data,function (result){
           if (result.status===200){
               doLoadOrderAndPage(result.data);
               doLoadClick();
           }
        });
    }
    function doLoadClick(){
        $(".btn").click(function (){
            let click = $(this).text();
            let order = $(this).parents(".houtai-order-list").data("order");
            if (click==="查看"){
                let url = `/shop/order`;
                let orderData = $("#mainContentId").data("orderData",order);
                $("#mainContentId").load(url);

            }else if (click==="删除"){
                if (!confirm("是否删除该订单?删除之后无法恢复"))
                    return ;
                let url = `/shop/deleteorder`;
                let orderId = order.orderId;
                let data = {"orderId":orderId};
                $.getJSON(url,data,function (result){
                    if (result.status===200){
                        alert("删除成功!");
                        doGetItems();
                    }
                });
            }else{
                return;
            }
        });
    }
    function doLoadOrderAndPage(orderAndPage){
        doinitPage(orderAndPage);
        doLoadOrders(orderAndPage.records);
    }
    function doLoadOrders(orders){
        let tbody = $("tbody");
        tbody.empty();
        for (let i = 0; i < orders.length; i++){
            let div1 = $(`<tr></tr>`);
            div1.append(doCreateOrderTitle(orders[i]));

            let div2 =$(`<tr class="houtai-order-list"></tr>`);
            div2.append(doCreateOrderDesc(i+1,orders[i]));
            div2.data("order",orders[i]);
            tbody.append(div1);
            tbody.append(div2);
            tbody.append(`<tr><td colspan="5"></td></tr>`);
        }
    }
    function doCreateOrderTitle(order){
        let status = order.status===1? "支付时间："+doStringtoDate(new Date(order.paytime)):"未付款";
        let created = doStringtoDate(new Date(order.created));
        return `<td class="houtai-orderId" colspan="5">
                    <p>创建时间：<span>${created}</span> 订单号：<span>${order.orderId}</span> <span>${status}</span> </p>
                </td>`;
    }
    function doCreateOrderDesc(i,order){

        return `<td>${i}</td>
                <td class="houtai-item-img">
                    <img src="${order.image}" />
                </td>
                <td>${order.username}</td>
                <td>${order.total}</td>
                <td>
                    <span class="houtai-order-button">
                        <button type="button" class="btn btn-warning">查看</button>
                        <button type="button" class="btn btn-danger">删除</button>
                    </span>
                </td>`;
    }
    function doStringtoDate(dateTime){
        let year = dateTime.getFullYear();
        let month = dateTime.getMonth()+1;
        let date = dateTime.getDate();
        let hours = dateTime.getHours();
        let min = dateTime.getMinutes();
        let seconds = dateTime.getSeconds();
        let dateTimes = year+"年"+month+"月"+date+"日 "+hours+"时"+min+"分"+seconds+"秒";
        return dateTimes;
    }
</script>
